function doSearchImTicket() {
	if (!validateBeforeSearch()) {
		return false;
	}
	var filterData = $("#filterForm").serialize();
	$.ajax({
		type:		"POST",
		url:		"../EwhValuator/DoSearchImTicketAjax?set_ajax_view",
		data : filterData,
		dataType:   "json",	
		beforeSend: function() {
		$('div#phaValuateLoaderDiv').show();		
	},
	success: 	function(data) {
		if (data.error.code ==  'OK') {
			//$('#applyValuateBtn').attr('disabled','disabled');
	        $("table#imticketTable tbody").html(data.result);			
		} else {
			displayError(data.error.mess);				
		}
		$('div#phaValuateLoaderDiv').hide();
	}
	});		

}
function validateBeforeSearch() {
	$('#seachStatusMessage').html('');

	if ($.trim($('#imTicketCodeFilter').val()) == ''
		&& $.trim($('#fromDateFiltermm').val()) == ''
			&& $.trim($('#fromDateFilterFilterdd').val()) == ''
				&& $.trim($('#fromDateFilterFilteryyyy').val()) == ''
					&& $.trim($('#toDateFiltermm').val()) == ''
						&& $.trim($('#toDateFilterdd').val()) == ''
							&& $.trim($('#toDateFilteryyyy').val()) == '')
	{
		$('#seachStatusMessage').html(_("Please enter search condition"));
		return false;
	}

	if (!($.trim($('#fromDateFiltermm').val()) == ''
		&& $.trim($('#fromDateFilterdd').val()) == ''
			&& $.trim($('#fromDateFilteryyyy').val()) == '')) {

		if (!checkDay($('#fromDateFilterdd'),true,'','#seachStatusMessage' )) {
			$('#seachStatusMessage').show();	
			return false;
		}	

		if (!checkMonth($('#fromDateFiltermm'),true,'','#seachStatusMessage')) {
			$('#seachStatusMessage').show();
			return false;
		} 

		var res 	= 	checkDate($('#fromDateFilteryyyy'),
				$('#fromDateFiltermm'),
				$('#fromDateFilterdd'), true, '', '#seachStatusMessage', false);
		if(!res){	
			$('#seachStatusMessage').show();
			return false;
		}
	}


	if (!($.trim($('#toDateFiltermm').val()) == ''
		&& $.trim($('#toDateFilterdd').val()) == ''
			&& $.trim($('#toDateFilteryyyy').val()) == '')) {

		if (!checkDay($('#toDateFilterdd'),true,'','#seachStatusMessage' )) {
			$('#seachStatusMessage').show();	
			return false;
		}	

		if (!checkMonth($('#toDateFiltermm'),true,'','#seachStatusMessage')) {
			$('#seachStatusMessage').show();
			return false;
		} 

		var res 	= 	checkDate($('#toDateFilteryyyy'),
				$('#toDateFiltermm'),
				$('#toDateFilterdd'), true, '', '#seachStatusMessage', false);	

		if(!res){	
			$('#seachStatusMessage').show();
			return false;
		}	
	}

	return true;
}

function valuateImticket(imTicketId, imTicketCode, imTicketCreatedDate) {

	$.ajax({
		type:		"POST",
		url:		"../EwhValuator/LoadImTicket4ValuateAjax?set_ajax_view",
		data : "imTicketId=" + imTicketId,
		dataType:   "json",	
		beforeSend: function() {
		$('div#phaValuateLoaderDiv').show();		
		},
		success: 	function(data) {
			if (data.error.code ==  'OK') {
				$("div#searchValuate").hide();
				$("div#valuatePanel").html(data.result);
				$("div#valuatePanel").show();				
				
				//$('input#valuatingImTicketId').val(imTicketId);					
         	    $('#applyValuateBtn').attr('disabled','');
         	    /*
         	    if (imTicketCode!=null || imTicketCreatedDate!=null) {
         	    	$("span#valuatorTitle").html(_("Valuate for import ticket [ {0}-{1} ]", imTicketCode, imTicketCreatedDate));
         	    }*/

			} else {
				displayError(data.error.mess);				
			}
			$('div#phaValuateLoaderDiv').hide();
		}
		});		
}

function applyValuation4ImTicket() {		
	
	if (!validateBeforeApplyValidating()) {
		return false;
	}
	var imTicketId=$("input#valuatingImTicketId").val();
	promptConfirm(_("Valuate drug/material"),_('Do you want to apply valuation for all drug/material in table bellow'), function() {
		submitValuation4ImTicket(imTicketId); 
		return true;
	},'YesNo','warn',_('No'));
		
}

function backToSearchPage() {
	promptConfirm(_("Back"),_('Are you sure go back to search page?'), function() {
		$("div#valuatePanel").html('');
		$("div#valuatePanel").hide();
		$("div#searchValuate").show();
		doSearchImTicket();
		return true;
	},'YesNo','warn',_('No'));
	
}

function submitFinalize(imTicketId) {
	$.ajax({
		type:		"POST",
		url:		"../EwhValuator/FinalizeImTicketAjax?set_ajax_view",
		data : 'imTicketId=' + imTicketId,
		dataType:   "json",	
		beforeSend: function() {
			$('div#phaValuateLoaderDiv').show();		
		},
		success: 	function(data) {
			if (data.error.code ==  'OK') {
				$("div#valuatePanel").html('');
				$("div#valuatePanel").hide();
				$("div#searchValuate").show();
				doSearchImTicket();			
										
			} else {
				displayError(data.error.mess);				
			}
			$('div#phaValuateLoaderDiv').hide();
		}
		});
	
}
function finalize(imTicketId) {
	promptConfirm(_("Finalize"),_('Do you want to finilize this import ticket?'), function() {
		submitFinalize(imTicketId);
		return true;
	},'YesNo','warn',_('No'));
}
function submitValuation4ImTicket(imTicketId) {
	var data = buildDataForValuating();	
	
	$.ajax({
		type:		"POST",
		url:		"../EwhValuator/ApplyValuationAjax?set_ajax_view",
		data : data,
		dataType:   "json",	
		beforeSend: function() {
		//$('div#phaValuateLoaderDiv').show();		
		},
		success: 	function(data) {
			if (data.error.code ==  'OK') {
				if (imTicketId != null) {
					//refresh to get latest result
					valuateImticket(imTicketId, null, null);
				}							
			} else {
				displayError(data.error.mess);				
			}
			//$('div#phaValuateLoaderDiv').hide();
		}
		});	
}



function validateBeforeApplyValidating() {
	var lcFloat = locale(decimalPoint, thousandSep, 2);
	
	var imTicketId=$("input#valuatingImTicketId").val();
	if ($.trim(imTicketId) == '') {
		return false;
	}
	
	if (!validateNumberByLocal($('#serviceFeeRateInp'), lcFloat, true, _('Invalid service fee'), 'valuateStatusMessage')) {
		return false;
	}
	
	if (!validateNumberByLocal($('#insurranceRateInp'), lcFloat, true, _('Invalid insurrance'), 'valuateStatusMessage')) {
		return false;
	}
	return true;
	
}

function buildDataForValuating() {
	var data = '';
	var lcNum = locale(decimalPoint, thousandSep, 2);	
	var sr = parseLcNum($('#serviceFeeRateInp').val(), lcNum) * serviceFeeRateInpSignv;
	var ir = parseLcNum($('#insurranceRateInp').val(), lcNum) * insurranceRateInpSignv;
	
	data += 'valuatedMethod=' + $('select#valuateMethodSel').val();
	data += '&imTicketId=' + $("input#valuatingImTicketId").val();
	data += '&serviceFeeRate=' + sr;
	data += '&insurranceRate=' + ir;
	data += '&rateUnit=' +  $("input[name='rateTypeRd']:checked").val();
	data += '&allow_sell_all=' + ($("#AllowSellAll").is(':checked') == true ? 1:0 );	
	
	
	$('table#drugMaterialTable tbody tr').each(function() {
		data += '&drugMaterialImportedId[]=' +$(this).attr('id');				
	});
	return data;
}

function allowSel(imTicketId, imTicketDetailId) {

	var allowSell = 0;
	 if ($("input#allowSellCheck" + imTicketDetailId).is(":checked")) {
		 allowSell = 1;
	 }
	
	$.ajax({
		type:		"POST",
		url:		"../EwhValuator/AllowSellAjax?set_ajax_view",
		data : "imTicketId=" + imTicketId + "&imTicketDetailId=" + imTicketDetailId
			+ "&allowSell=" + allowSell,
		dataType:   "json",		
		beforeSend: function() {
			//document.body.style.cursor = 'wait';
			$('div#phaValuateLoaderDiv').show();	
		},
		success: 	function(data) {
			$('div#phaValuateLoaderDiv').hide();	
			if (data.error.code ==  'OK') {							
			} else {
				displayError(data.error.mess);				
			}	
			
		}
		});		
}

